namespace BM.Tools.WebControls.MForm.Generator
{
    using System.Xml.Schema;

    using BM.Tools.WebControls.MForm.Controls.Items;
    using BM.Tools.Xml.Schema;

    /// <summary>
    /// FormChoice is an intermediate class that is created after visiting <see cref="XmlSchemaChoice" /> node
    /// or in case of a <see cref="XmlSchemaElement" /> that is abstract.
    /// </summary>
    public class FormChoice : FormItem
    {        
        private int choiceIndex;

        /// <summary>
        /// Initializes a new instance of the <see cref="FormChoice"/> class. 
        /// </summary>
        /// <param name="builder">
        /// The builder.
        /// </param>
        /// <param name="parent">
        /// The parent.
        /// </param>
        /// <param name="schemaObject">
        /// The schemaObject.
        /// </param>
        public FormChoice(FormBuilder builder, Visited parent, XmlSchemaObject schemaObject) : base(builder, parent, schemaObject)
        {
        }

        /// <summary>
        /// Gets the xml schema choice object, if the FormChoice was created from such an object.
        /// </summary>
        /// <value>
        /// The xml schema choice object.
        /// </value>
        public XmlSchemaChoice Choice
        {
            get
            {
                return schemaObject as XmlSchemaChoice;
            }
        }

        /// <summary>
        /// Gets the index of this object, being an ordinal number of all <see cref="FormChoice" /> objects in its parent.
        /// </summary>
        /// <value>
        /// The choice index.
        /// </value>
        public int ChoiceIndex
        {
            get
            {
                if (Choice != null && choiceIndex == 0)
                {
                    choiceIndex = 1;
                    foreach (Visited v in parent.children)
                    {
                        if (v == this)
                        {
                            break;
                        }

                        if (v.schemaObject is XmlSchemaChoice)
                        {
                            choiceIndex++;
                        }
                    }
                }

                return choiceIndex;
            }
        }

        /// <summary>
        /// Gets LocalName.
        /// </summary>
        /// <value>
        /// The local name.
        /// </value>
        public override string LocalName
        {
            get
            {
                if (Choice != null)
                {
                    return ChoiceItem.ChoiceItemCssClass + ChoiceIndex;
                }

                return base.LocalName;
            }
        }

        /// <summary>
        /// Gets MinOccurs.
        /// </summary>
        /// <value>
        /// The min occurs.
        /// </value>
        public override decimal MinOccurs
        {
            get
            {
                if (Builder.Settings.IgnoreNonLeafMinOccurs && !IsLeaf)
                {
                    return 0;
                }

                return base.MinOccurs;
            }
        }

        /// <summary>
        /// Gets MaxOccurs from schema.
        /// </summary>
        protected override decimal MaxOccursFromSchema
        {
            get
            {
                if (Choice != null)
                {
                    return Choice.MaxOccurs;
                }

                return base.MaxOccursFromSchema;
            }
        }
    }
}